package com.ruoyi.web.controller.reserve;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.reserve.domain.Checkitem;
import com.ruoyi.reserve.service.impl.ICheckItemsService;
import com.ruoyi.system.domain.SysPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@RestController
@RequestMapping("/reserve/checkItems")
public class CheckItemsController extends BaseController {
    @Autowired
    private ICheckItemsService checkItemsService;



    /**
     * 获取检查项列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Checkitem checkitem)
    {
        startPage();
        List<Checkitem> list = checkItemsService.getList(checkitem);
        TableDataInfo tableDataInfo = getDataTable(list);
        return tableDataInfo;
    }


    /**
     * 删除检查项
     */
    @Log(title = "检查项管理", businessType = BusinessType.UPDATE)
    @DeleteMapping("/{itemsIds}")
    public AjaxResult remove(@PathVariable Integer[] itemsIds)
    {
        return toAjax(checkItemsService.deleteItemsByIds(itemsIds));
    }


    /**
     * 新增岗位
     */
    @Log(title = "检查项管理", businessType = BusinessType.INSERT)
    @PostMapping("/addItem")
    public AjaxResult add(@RequestBody Checkitem checkitem)
    {
        if (checkItemsService.getInfoByCode(checkitem.getCode()) != null ) {
            return AjaxResult.error("该编号已存在");
        }
        if (checkItemsService.getInfoByName(checkitem.getName()) != null) {
            return AjaxResult.error("该名称已存在");
        }
       return toAjax(checkItemsService.insertItems(checkitem));
    }

    /**
     * 根据检查项id获取详情
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo (@PathVariable Integer id) {
        return AjaxResult.success(checkItemsService.getInfo(id));
    }

    /**
     * 更新
     */
    @Log(title = "检查项管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateItem")
    public AjaxResult updateItem(@RequestBody Checkitem checkitem)
    {
        Checkitem checkitemIsCode = checkItemsService.getInfoByCode(checkitem.getCode());
        if (checkitemIsCode != null ) {
            if(!checkitem.getId().equals(checkitemIsCode.getId())){
                return AjaxResult.error("该编号已存在");
            }
        }

        Checkitem checkitemIsName = checkItemsService.getInfoByName(checkitem.getName());
        if (checkitemIsName != null ) {
            if(!checkitem.getId().equals(checkitemIsName.getId())){
                return AjaxResult.error("该名称已存在");
            }
        }
        return toAjax(checkItemsService.updateItems(checkitem));
    }
}
